﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using FacebookWrapper.ObjectModel;

namespace AutomaticAdvertising
{
    public class DataHolder
    {
        private List<PostData> m_PostsList;

        public int Count
        {
            get { return m_PostsList.Count; }
        }

        public DataHolder()
        {
            m_PostsList = new List<PostData>();
        }

        public void Init()
        {
            readFromFile();
        }

        public void Add(PostData i_PostData)
        {
            m_PostsList.Add(i_PostData);

            saveToFile();
        }

        public void Remove(PostData i_PostData)
        {
            m_PostsList.Remove(i_PostData);

            saveToFile();
        }

        public void RemoveAt(int i_Index)
        {
            m_PostsList.RemoveAt(i_Index);

            saveToFile();
        }

        public void Insert(int i_Index, PostData i_PostData)
        {
            m_PostsList.Insert(i_Index, i_PostData);

            saveToFile();
        }

        public int IndexOf(PostData i_PostData)
        {
            return m_PostsList.IndexOf(i_PostData);
        }

        public void MergeWithLocal(Post i_Post)
        {
            foreach (Album albom in i_Post.From.Albums)
            {
                foreach (Photo photo in albom.Photos)
                {
                    PostData postData = m_PostsList.Find(post => post.Id == photo.Id);

                    if (postData != null)
                    {
                        postData.Status = PostData.eStatus.posted;
                        postData.Photo = photo;
                    }
                }
            }

            saveToFile();
        }

        public System.Collections.IEnumerator GetEnumerator()
        {
            foreach (PostData item in m_PostsList)
            {
                yield return item;
            }
        }

        private void saveToFile()
        {
            List<PostData> newListOfPosts = new List<PostData>();
            
            foreach (PostData postData in m_PostsList)
            {
                if (postData.Status != PostData.eStatus.deleted)
                {
                    newListOfPosts.Add(postData);
                }
            }

            System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(newListOfPosts.GetType());

            System.IO.StreamWriter file = new System.IO.StreamWriter(@"SerializationOverview.xml");

            writer.Serialize(file, newListOfPosts);
            file.Close();
        }

        private void readFromFile()
        {
            System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(m_PostsList.GetType());

            if (File.Exists(@"SerializationOverview.xml"))
            {
                System.IO.StreamReader file = new System.IO.StreamReader(@"SerializationOverview.xml");

                m_PostsList = (List<PostData>)reader.Deserialize(file);
            }
        }
    }
}
